package com.lhy.controller;

import com.lhy.domain.Drugs;
import com.lhy.domain.User;
import com.lhy.service.UserService;
import com.lhy.utils.Pagination;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/findOne")
    public String findOneById(HttpServletRequest req,HttpServletRequest resp){
        Integer id=Integer.parseInt(req.getParameter("id"));
        HttpSession session= req.getSession();
        session.setAttribute("id",id);
        return "adminmodify";
    }
    @RequestMapping("/goOut")
    public String goOut(HttpServletRequest req,HttpServletRequest resp){
        HttpSession session = req.getSession();
        session.invalidate();
        return "redirect:/login";
    }
    @RequestMapping("/login")
    public String getLoginUser(HttpServletRequest req,HttpServletRequest resp){
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=null;
        try {
            user=userService.findUser(username,password);
        }catch (Exception e){
            e.printStackTrace();
            return "login";
        }
        if (user!=null){
            HttpSession session=req.getSession();
            session.setAttribute("user",user);
            return "index";
        }
        HttpSession session=req.getSession();
        session.setAttribute("error","用户名或密码错误");
        return "login";
    }
    @RequestMapping("/update")
    public String updatePassword(HttpServletRequest req,HttpServletRequest resp){
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        userService.updatePassword(password1,password2);
        return "index";
    }

    @RequestMapping("/findSomeUser")
    public String findSomeUser(User user, Pagination<User> page,HttpServletRequest req,HttpServletRequest resp){
        page.setQueryObject(user);
        List<User> users = userService.findSomeUser1(page);
        HttpSession session=req.getSession();
        session.setAttribute("userList",page);
        return "adminlist";
    }
    @RequestMapping("/add")
    public String addUser(HttpServletRequest req,HttpServletRequest resp){
        Map<String, String[]> map = req.getParameterMap();
        User u=new User();
        try {
            BeanUtils.populate(u,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userService.addUser(u);
        return "adminlist";
    }
    @RequestMapping("/delete")
    public String deleteUser(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        userService.deleteUser(id);
        return "main";
    }
    @RequestMapping("/updateUser")
    public String updateUser(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String[]> map_user = req.getParameterMap();
        User updateUser=new User();
        try {
            BeanUtils.populate(updateUser,map_user);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userService.updateUser(updateUser);
        return "main";
    }
}
